import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_screenutil/screen_util.dart';
import 'package:study_online/global/colors.dart';
import 'package:study_online/global/myIcons.dart';
import 'package:study_online/entity/Problem.dart';

import 'ImgsContainer.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.black26,
          title: Text('container'),
        ),
        body: ProblemItem(),
      ),
    );
  }
}

class ProblemItem extends StatefulWidget {
  ProblemItem({Key key, this.problem, this.index}) : super(key: key);
  Problem problem;
  int index;

  @override
  _ProblemItemState createState() {
    return _ProblemItemState();
  }
}

class _ProblemItemState extends State<ProblemItem> {
  List<String> _labelList = [];
  List<String> _imgUrl=[];
  @override
  void initState() {
    super.initState();
    _problem = widget.problem;
    String tp = _problem.content;
    int num = 0;
    var pos = [0, 0];
    // for (int i = 0; i < tp.length; ++i) {
    //   if (tp[i] == '#') {
    //     pos[num % 2] = i;
    //     num++;
    //     if (num >= 2)
    //       _labelList
    //           .add(tp.substring(min(pos[0], pos[1]) + 1, max(pos[0], pos[1])));
    //   }
    // }
    var temp="";
    for (int i = 0; i < tp.length; ++i) {
      if (tp[i] == '#'||tp[i]==' ') {
        if(i!=0)
          _labelList.add(temp);
        temp="";
      }
      else
        temp+=tp[i];
      if(tp[i]==' ') {
        _problem.content=tp.substring(i+1);
        break;
      }
    }
    int pos1=_problem.content.indexOf("@@");
    if(pos1!=-1) {
      String tp1 = _problem.content.substring(pos1 + 2);
      _problem.content = _problem.content.substring(0, pos1);
      _imgUrl = tp1.split("@@");
     // print(_imgUrl);
    }
  }

  @override
  void dispose() {
    super.dispose();
  }

  Problem _problem = new Problem(
      "windChaser",
      "1",
      "#算法#flutter#回溯法 flutter用起来真舒服 "+
          "@@https://img0.baidu.com/it/u=1479071042,4089030983&fm=26&fmt=auto",
      12,
      3,
      "2021-10-9",
      "https://img2.baidu.com/it/u=172917783,2425047487&fm=26&fmt=auto");

  Widget _labelWidget(String name) {
    return Container(
      alignment: Alignment.center,
      height: ScreenUtil().setHeight(54),
      //width: 90,
      margin: EdgeInsets.only(right: 7),
      decoration: BoxDecoration(
        color: Color(0xffF5C5A8),
        borderRadius: BorderRadius.only(
          bottomLeft: Radius.circular(ScreenUtil().setHeight(27)),
          bottomRight: Radius.circular(ScreenUtil().setHeight(27)),
          topRight: Radius.circular(ScreenUtil().setHeight(27)),
        ),
      ),
      padding: EdgeInsets.only(left: 6, right: 6),
      child: Text(
        '#$name',
        style: TextStyle(
          fontSize: ScreenUtil().setSp(31),
          color: Colors.white,
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    //debugPaintSizeEnabled=true;
    ScreenUtil.init(
      BoxConstraints(
          maxWidth: MediaQuery.of(context).size.width,
          maxHeight: MediaQuery.of(context).size.height),
      designSize: Size(1080, 2340),
    );
    return InkWell(
      onTap: () {
        Navigator.pushNamed(context, "problemDetail",
            arguments: [widget.problem]);
      },
      child: Container(
        color: Colors.white,
        width: ScreenUtil().screenWidth,
        margin: EdgeInsets.only(
          bottom: ScreenUtil().setHeight(28),
        ),
        padding: EdgeInsets.only(
          left: ScreenUtil().setWidth(55),
          right: ScreenUtil().setWidth(55),
          top: ScreenUtil().setHeight(28),
        ),
        child: Column(
          //mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              //crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                ClipOval(
                  child: Image.network(_problem.photo,
                      width: ScreenUtil().setSp(126),
                      height: ScreenUtil().setSp(126),
                      fit: BoxFit.cover),
                ),
                SizedBox(width: ScreenUtil().setWidth(13)),
                Container(
                  height: ScreenUtil().setHeight(140),
                  margin: EdgeInsets.only(left: ScreenUtil().setWidth(20)),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: [
                      Text(
                        _problem.name,
                        style: TextStyle(
                          fontSize: ScreenUtil().setSp(46),
                          color: Colors.black,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      Text(
                        _problem.time.substring(0,16),
                        style: TextStyle(
                          fontSize: ScreenUtil().setSp(40),
                          color: AppColors.subtext,
                        ),
                      ),
                    ],
                  ),
                ),
                Expanded(
                  child: Align(
                    alignment: Alignment.bottomRight,
                    child: PopupMenuButton(
                      padding: EdgeInsets.zero,
                      itemBuilder: (BuildContext context) {
                        return <PopupMenuItem<String>>[
                          PopupMenuItem<String>(
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                              children: [
                                Icon(MyIcons.guanzhu),
                                Text("关注"),
                              ],
                            ),
                            value: "share",
                          ),
                          PopupMenuItem<String>(
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                              children: [
                                Icon(MyIcons.sixin),
                                Text("私信"),
                              ],
                            ),
                            value: "more",
                          ),
                        ];
                      },
                      onSelected: (String action) {
                        switch (action) {
                          case "share":
                            print(widget.problem.replyNum);
                            break;
                          case "more":
                            print("更多");
                            break;
                        }
                      },
                    ),
                  ),
                )
              ],
            ),
            Container(
              margin: EdgeInsets.only(
                  //left: ScreenUtil().setWidth(98),
                  top: ScreenUtil().setHeight(46)),
              child: Column(
                //mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: _labelList
                        .asMap()
                        .keys
                        .map((index) => _labelWidget(_labelList[index]))
                        .toList(),
                  ),
                  Container(
                    alignment: Alignment.bottomLeft,
                    padding: EdgeInsets.only(right: 17),
                    child: Text(
                      _problem.content,
                      style: TextStyle(
                        fontSize: ScreenUtil().setSp(46),
                        color: Colors.black,
                      ),
                    ),
                  ),
                  SizedBox(height: ScreenUtil().setHeight(38)),
                  ImgContainer(imgUrl: _imgUrl,),
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Expanded(
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Row(
                              children: [
                                IconButton(
                                    icon: Icon(
                                      MyIcons.thumb_up_alt,
                                      size: ScreenUtil().setSp(55),
                                      color: AppColors.subtext,
                                    ),
                                    onPressed: () {}),
                                Text(
                                  _problem.thumbsUpNum.toString(),
                                  style: TextStyle(
                                    fontSize: ScreenUtil().setSp(40),
                                    color: AppColors.subtext,
                                  ),
                                ),
                              ],
                            ),
                            Row(
                              children: [
                                IconButton(
                                  icon: Icon(
                                    MyIcons.remark,
                                    size: ScreenUtil().setSp(75),
                                    color: AppColors.subtext,
                                  ),
                                  onPressed: () {},
                                ),
                                Text(
                                  _problem.replyNum.toString(),
                                  style: TextStyle(
                                    fontSize: ScreenUtil().setSp(40),
                                    color: AppColors.subtext,
                                  ),
                                ),
                              ],
                            ),
                            //SizedBox(width: ScreenUtil().setWidth(30)),
                            IconButton(
                              icon: Icon(
                                Icons.share_outlined,
                                size: ScreenUtil().setSp(75),
                                color: AppColors.subtext,
                              ),
                              onPressed: () {},
                            ),
                          ],
                        ),
                      )
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
